package com.xyzq.hs.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ParameterUtils {
	
	static Logger log=LoggerFactory.getLogger(ParameterUtils.class);
	
	public static String buildParam(Object obj){
		if(obj==null)
			return "";
		Field[] fields = obj.getClass().getDeclaredFields();
		int len = fields.length;
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<len;i++){
			Field field = fields[i];
			int mod = field.getModifiers();
			if(Modifier.isFinal(mod)||!Modifier.isPublic(mod)){
				log.debug("Field '"+field.getName()+"' modifier is not public or is final,skipped.");
				continue;
			}
			try {
				Object fieldVal=field.get(obj);
				if(fieldVal==null){
					log.debug("Field  '"+field.getName()+"' value is null,skipped.");
					continue;
				}
				sb.append(StringUtils.propertyNameToFieldName(field.getName()))
				.append("=")
				.append(fieldVal.toString())
				.append("&");
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		String s = sb.toString();
		if(StringUtils.isBlank(s)){
			log.debug("Parameter is null.");
			return null;
		}
		s=s.substring(0, sb.toString().length()-1);
		log.info("Parameter:"+s);
		return s;
	}

}
